package com.lijing.mock;

import com.lijing.mock.constract.MongoTableConstant;
import com.lijing.mock.model.dto.UrlBaseDTO;
import com.mongodb.client.model.IndexOptions;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * mongodb 表初始化
 * @author by lijing
 * @date 2019/12/5 0005 17:06
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes={Application.class})
public class MongodbTableInit {
    /** mongodb注入 */
    @Autowired
    private MongoTemplate mongoTemplate;

    @Test
    public void initTable(){

        mongoTemplate.createCollection(MongoTableConstant.BASE_URL_CONFIG);
        mongoTemplate.getCollection(MongoTableConstant.BASE_URL_CONFIG).createIndex(new Document().append("url",1).append("projectName",1), new IndexOptions().unique(true).name("url_project_unique"));

        mongoTemplate.createCollection(MongoTableConstant.URL_MOCK_RESULT_DATA);
        mongoTemplate.getCollection(MongoTableConstant.URL_MOCK_RESULT_DATA).createIndex(new Document("urlId",1),new IndexOptions().name("urlId_index"));

    }

    @Test
    public void testUnique(){
        UrlBaseDTO urlBaseDTO = new UrlBaseDTO();
        urlBaseDTO.setUrl("/test/url1");
        urlBaseDTO.setQuestMethod("post");
        urlBaseDTO.setProjectName("testProject");
        mongoTemplate.save(urlBaseDTO,MongoTableConstant.BASE_URL_CONFIG);
    }

    @Test
    public void dropCollection(){
        mongoTemplate.dropCollection(MongoTableConstant.BASE_URL_CONFIG);
        mongoTemplate.dropCollection(MongoTableConstant.URL_MOCK_RESULT_DATA);
    }

}
